package com.darwin.darwinDisplay
{
	import com.darwin.darwinBitmapEngine.core.Canvas;
	import com.darwin.darwinUI.core.UIComponent;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	[Event(name="darwin_draw_complete",type="com.darwin.darwinDisplay.DarBitmapMovieNodeEvent")]
	[Event(name="darwin_play_complete",type="com.darwin.darwinDisplay.DarBitmapMovieNodeEvent")]
	/**
	 * 可添加在UI层中的dar动画对象,用法同DarBitmapMovieNode类似
	 * @author Roland
	 */
	public class DarBitmapMovieClip extends UIComponent
	{
		private var canvas:Canvas;
		private var darMovieNode:DarBitmapMovieNode;
		public function DarBitmapMovieClip(source:* = null)
		{
			super();
			darMovieNode = new DarBitmapMovieNode(source);
			darMovieNode.addEventListener(DarBitmapMovieNodeEvent.DARWIN_DRAW_COMPLETE,darCompleteHandler);
			darMovieNode.addEventListener(DarBitmapMovieNodeEvent.DARWIN_PLAY_COMPLETE,darPlayComplete);
		}
		
		protected function darPlayComplete(event:Event):void
		{
			dispatchEvent(event);
		}
		
		protected function onFrame(event:Event):void
		{
			if(canvas)
			{
				canvas.render();
			}
		}
		
		private function darCompleteHandler(e:Event):void
		{
			darMovieNode.removeEventListener(DarBitmapMovieNodeEvent.DARWIN_DRAW_COMPLETE,darCompleteHandler);
			if(darMovieNode.bitmapData==null)return;
			var rect:Rectangle = getFramesBounds(darMovieNode.bitmapDataList,darMovieNode.frameOffsetList);
			canvas = new Canvas(rect.width,rect.height);
			canvas.addNode(darMovieNode);
			addChild(canvas);
			dispatchEvent(e);
			
			canvas.render();//先渲染一次
			if(darMovieNode.totalFrames > 1)
			{
				addEventListener(Event.ENTER_FRAME,onFrame);
			}
			dispatchEvent(e);
		}
		
		private function getFramesBounds(frames:Array,offsetList:Array):Rectangle
		{
			var maxRect:Rectangle = new Rectangle(0,0,1,1);
			for (var i:int = 0; i < frames.length; i++) 
			{
				var bitmapData:BitmapData = frames[i];
				var rect:Rectangle = new Rectangle(offsetList[i].x,offsetList[i].y,bitmapData.rect.width,bitmapData.rect.height);
				maxRect = maxRect.union(rect);
			}
			return maxRect;
		}
		
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(type, listener, useCapture, priority, useWeakReference);
			if(type == DarBitmapMovieNodeEvent.DARWIN_DRAW_COMPLETE)
			{
				if(darMovieNode.isComplete)
				{
					dispatchEvent(new DarBitmapMovieNodeEvent(DarBitmapMovieNodeEvent.DARWIN_DRAW_COMPLETE));
				}
			}
		}
		
		override public function get width():Number
		{
			return canvas ? canvas.width : 0;
		}
		
		override public function get height():Number
		{
			return canvas ? canvas.height : 0;
		}
		
		public function get frameRate():int
		{
			return darMovieNode.frameRate;
		}
		
		public function set frameRate(value:int):void
		{
			darMovieNode.frameRate = value;
		}
		
		
		public function get offsetPoint():Point
		{
			return darMovieNode ? darMovieNode.offsetPoint : null;
		}
		
		public function gotoAndStop(frame:int):void
		{
			if(darMovieNode)
			{
				darMovieNode.gotoAndStop(frame);
			}
		}
		
		public function gotoAndPlay(frame:int):void
		{
			if(darMovieNode)
			{
				darMovieNode.gotoAndPlay(frame);
			}
		}
		
		public function stop():void
		{
			if(darMovieNode)
			{
				darMovieNode.stop();
			}
		}
		
		public function play():void
		{
			if(darMovieNode)
			{
				darMovieNode.play();
			}
		}
		
		/**
		 *	销毁动画资源，释放内存
		 */
		public function destroy():void
		{
			if(darMovieNode)
			{
				darMovieNode.destroy();
			}
			canvas = null;
			darMovieNode = null;
		}
		
	}
}